import { getRecheckRequestList, 
  getStudentPaper, 
  rejectTheRequest, 
  getRejectReason,
  getSubjectTeachers,
  RecheckAssignment,
  getRecheckResult,
  recoverRecheckResult
} from '@/api/server/recheckRequestManage'
import { useServerUserStore } from '@/stores/modules/user-server'
import { ref } from 'vue'

export function   useRecheck() {

  const recheckRequestList = ref([])
  const total = ref(0)
  const page = ref(1)
  const pageSize = ref(10)
  const status = ref('所有状态')

  const userStore = useServerUserStore()

  const getRecheckRequestListOfHook = async () => {
    const res = await getRecheckRequestList({
        userId: userStore.userInfo.id,
        page: page.value  ,
        pageSize: pageSize.value,
        status: status.value
    })
    recheckRequestList.value = res.data.data.list
    total.value = res.data.data.total
  }

  const studentPaperDialogVisible = ref(false)
  const studentPaperResult = ref(null)
  const recheckRequestId = ref(null)
  const studentPaperId = ref(null)
  const requestTeacherId = ref(null)

  const openStudentPaper = async (row) => {
    recheckRequestId.value = row.recheckRequestId
    studentPaperId.value = row.studentPaperId
    requestTeacherId.value = row.requestTeacherId
    const data = {
      recheckRequestId: recheckRequestId.value,
      studentPaperId: studentPaperId.value,
      requestTeacherId: requestTeacherId.value
    }
    const res = await getStudentPaper(data)
    studentPaperDialogVisible.value = true
    studentPaperResult.value = res.data.data
  }

  // 打开驳回的对话框
  const rejectDialogVisible = ref(false)
  const openRejectDialog = () => {
    rejectDialogVisible.value = true
  }
  const rejectReason = ref('')
  const handleReject = async () => {
    rejectReason.value = ''
    if(!rejectReason.value){
      ElMessage.warning('请输入驳回原因')
      return
    }
    const res = await rejectTheRequest({
      recheckRequestId: recheckRequestId.value,
      requestTeacherId: requestTeacherId.value,
      studentPaperId: studentPaperId.value,
      rejectReason: rejectReason.value
    })
    if(parseInt(res.data.code) === 200){
      ElMessage.success('驳回成功')
      rejectDialogVisible.value = false
      studentPaperDialogVisible.value = false
      rejectReason.value = ''
      getRecheckRequestListOfHook()
    }
  }

  const rejectReasonDialogVisible = ref(false)
  const getRejectReasonOfHook = async (row) => {
    const res = await getRejectReason({
      studentPaperId: row.studentPaperId
    })
    rejectReason.value = res.data.data
    rejectReasonDialogVisible.value = true
  }

  // 给教师分配复查任务
  // 打开分配教师的对话框
  const subjectTeachers = ref([])
  const selectedTeachers = ref([])
  const filteredTeachers = ref([])
  const teacherDialogVisible = ref(false)
  const recheckRequestIdOfTeacherAssignment = ref(null)
  const studentPaperIdOfTeacherAssignment = ref(null)
  const openTeacherDialog = async (row) => {
    recheckRequestIdOfTeacherAssignment.value = row.recheckRequestId
    studentPaperIdOfTeacherAssignment.value = row.studentPaperId
    const res = await getSubjectTeachers({
      userId: userStore.userInfo.id
    })
    subjectTeachers.value = res.data.data
    filteredTeachers.value = subjectTeachers.value
    teacherDialogVisible.value = true
    console.log('subjectTeachers:',subjectTeachers.value)
  }

  const handleTeacherAssignment = async () => {
    const selectedTeacherIds = selectedTeachers.value.map(teacher => teacher.id)
    const data = {
      recheckRequestId: recheckRequestIdOfTeacherAssignment.value,
      studentPaperId: studentPaperIdOfTeacherAssignment.value,
      selectedTeacherIds: selectedTeacherIds
    }
  
    const res = await RecheckAssignment(data)
    if(parseInt(res.data.code) === 200){
      ElMessage.success('分配成功')
      teacherDialogVisible.value = false
      getRecheckRequestListOfHook()
    }
  }

  // 查看复审结果
  const recheckResultDialogVisible = ref(false)
  const recheckResult = ref(null)
  const resultSummary = ref('')
  const getRecheckResultOfHook = async (row) => {
    resultSummary.value = ''
    const res = await getRecheckResult({
      recheckRequestId: row.recheckRequestId
    })
    recheckResult.value = res.data.data
    recheckResultDialogVisible.value = true
  }

  const recoverRecheckResultOfHook = async () => {
    const res = await recoverRecheckResult({
      recheckRequestId: recheckResult.value.recheckRequestId,
      originalScore: recheckResult.value.originalScore,
      finalScore: recheckResult.value.finalScore,
      resultSummary: resultSummary.value,
      subjectLeaderId: recheckResult.value.subjectLeaderId,
      requestTeacherId: recheckResult.value.requestTeacherId,
      studentPaperId: recheckResult.value.studentPaperId
    })
    if(parseInt(res.data.code) === 200){
      ElMessage.success('发送复审结果成功！')
      recheckResultDialogVisible.value = false
      getRecheckRequestListOfHook()
    }
  }

  return {

    // 获得查卷申请列表
    recheckRequestList,
    total,
    page,
    pageSize,
    status,
    getRecheckRequestListOfHook,

    // 打开学生试卷
    studentPaperDialogVisible,
    studentPaperResult,
    openStudentPaper,

    // 打开驳回的对话框
    rejectDialogVisible,
    openRejectDialog,

    // 驳回原因
    rejectReason,

    // 驳回
    handleReject,

    // 查看驳回原因
    getRejectReasonOfHook,

    // 查看驳回原因的对话框
    rejectReasonDialogVisible,

    // 获取教师列表
    subjectTeachers,
    filteredTeachers,
    openTeacherDialog,

    // 分配教师对话框
    teacherDialogVisible,
    subjectTeachers,
    selectedTeachers,
    openTeacherDialog,

    // 分配教师
    handleTeacherAssignment,

    // 查看复审结果
    recheckResultDialogVisible,
    recheckResult,
    getRecheckResultOfHook,

    // 复审总结
    resultSummary,
    recoverRecheckResultOfHook
  }
}
